package problem43;

//43.字符串相乘
//https://leetcode.cn/problems/multiply-strings/description/

class Solution {
    public String multiply(String num1, String num2) {
        char[] s1 = new StringBuffer(num1).reverse().toString().toCharArray();
        char[] s2 = new StringBuffer(num2).reverse().toString().toCharArray();
        int n = s1.length, m = s2.length;
        int[] tmp = new int[n+m-1];
        for(int i = 0; i<n; i++) {
            for(int j = 0; j<m; j++) {
                tmp[i+j] += (s1[i]-'0') * (s2[j]-'0');
            }
        }
        //处理进制
        int jz = 0;
        StringBuffer ret = new StringBuffer();
        for(int i = 0; i<n+m-1; i++) {
            int s = (tmp[i] + jz) % 10;
            jz = (tmp[i] + jz) / 10;
            ret.append(s);
        }
        while(jz > 0) {
            ret.append(jz%10);
            jz /= 10;
        }
        //处理前导0
        while(ret.length() > 1 && ret.charAt(ret.length()-1) == '0') {
            ret.deleteCharAt(ret.length()-1);
        }
        return ret.reverse().toString();
    }
}